package com.zzy.accessdecision.controoler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzy.accessdecision.mapper.ResourceMapper;
import com.zzy.accessdecision.mapper.UserMapper;
import com.zzy.accessdecision.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Lucas-zhang
 * @description TODO
 * @date 2022-03-25 14:39
 */
@RestController
public class RegisterController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/register")
    public String register(@RequestBody  List<User> users) throws JsonProcessingException {
        for (User user : users) {
            String password = new BCryptPasswordEncoder().encode(user.getPassword());
            user.setPassword(password);
            userMapper.insertOneUser(user);
        }

        return new ObjectMapper().writeValueAsString(users);
    }

}
